TOOL.Category		= "(WarMelons)"
TOOL.Name			= "#Barracks"
TOOL.Command		= nil
TOOL.ConfigName		= ""

TOOL.ClientConVar[ "teamnumber" ] = "1"
TOOL.ClientConVar[ "gravity" ] = "1"
TOOL.ClientConVar[ "melongravity" ] = "1"
TOOL.ClientConVar[ "moving" ] = "1"
TOOL.ClientConVar[ "welded" ] = "1"
TOOL.ClientConVar[ "followrange" ] = "0"

if (CLIENT) then
	language.Add( "Tool_barrax_name", "Barracks" )
	language.Add( "Tool_barrax_desc", "Spawns weak melons to provide fire support" )
	language.Add( "Tool_barrax_0", "Left-click to spawn a barracks. Right-click to display cost." )
	language.Add( "Undone_barrax", "Undone Warmelon" )
end

function TOOL:LeftClick (trace)
if ( SERVER ) then
	local entz = ents.FindByClass("lap_spawnpoint");
	local ply = self:GetOwner()
	local disfin = 0
	local Pos = trace.HitPos
	local Ang = trace.HitNormal
	local buildradius = server_settings.Int( "WM_BuildRadius", 750 )
	local cteam = 0
	if ply:GetNetworkedInt("melonteam") ~= 0 then
	cteam = ply:GetNetworkedInt("melonteam")
	else
	cteam = self:GetClientNumber("teamnumber")
	end
	if buildradius != 0 then
	for k, v in pairs(entz) do
	local dist = v:GetPos():Distance(Pos);
		if (v.Team == cteam && dist <= buildradius) then
		disfin = 1
		end
	end
	else
	disfin = 1
	end
	local nrgsup = ply:GetNetworkedInt( "nrg" )
	local toolcost = server_settings.Int( "WM_Toolcost", 1 )
	local cost = (10000-self:GetClientNumber("melongravity")*4000-self:GetClientNumber("gravity")*2000)*toolcost
if (trace.Hit && !trace.HitNoDraw && nrgsup >= cost) then
	if ( disfin == 1 ) then
	ply:SetNetworkedInt( "nrg" , nrgsup - cost)
	melon = ents.Create ("johnny_barracks");
	melon:SetPos (trace.HitPos + trace.HitNormal * 1);
	melon:SetAngles (trace.HitNormal:Angle());
	melon:GetTable().Team = cteam;
	melon:GetTable().Grav = self:GetClientNumber("gravity") == 1;
	melon:GetTable().MelonGrav = self:GetClientNumber("melongravity") == 1;
	melon:GetTable().FollowRange = self:GetClientNumber("followrange");
	melon:Spawn ();
	melon:Activate ();
	melon:GetPhysicsObject():EnableGravity(self:GetClientNumber("gravity") == 1);
	if (self:GetClientNumber("welded") == 1) then upright = constraint.Weld (melon, trace.Entity, 0, trace.PhysicsBone, 0, true) end
	trace.Entity:DeleteOnRemove(melon);
	undo.Create("barrax");
    undo.AddEntity (melon);
	if (self:GetClientNumber("welded") == 1) then undo.AddEntity (upright) end
    undo.SetPlayer (self:GetOwner());
	undo.Finish();
	return true;
	else
	ply:PrintMessage(HUD_PRINTTALK, "Too far from spawn point")
	end
	else 
	local message = "Your NRG:" .. ply:GetNetworkedInt( "nrg" ) .. " Cost: " .. cost
	ply:PrintMessage(HUD_PRINTTALK, message)
	return false end
end
end

function TOOL:RightClick (trace)
if (SERVER) then
local ply = self:GetOwner()
local toolcost = server_settings.Int( "WM_Toolcost", 1 )
local cost = (10000-self:GetClientNumber("melongravity")*4000-self:GetClientNumber("gravity")*2000)*toolcost

	local message = "Your NRG:" .. ply:GetNetworkedInt( "nrg" ) .. " Cost: " .. cost
	ply:PrintMessage(HUD_PRINTTALK, message)
end
	return true
end

function TOOL.BuildCPanel (CPanel)
	CPanel:AddControl ("Header", { Text="#Tool_barrax_name", Description="#Tool_barrax_desc" })
	CPanel:AddControl ("Slider", {
		Label = "Team number",
		Command = "barrax_teamnumber",
		Type = "Integer",
		Min = "1",
		Max = "6"
	} )
	CPanel:AddControl ("Slider", {
		Label = "Following Range (0 for none)",
		Command = "barrax_followrange",
		Type = "Float",
		Min = "0",
		Max = "4096"
	} )
	CPanel:AddControl( "Checkbox", {
		Label = "Gravity toggle",
		Description = "Uncheck box to make barracks have no gravity",
		Command = "barrax_gravity"
	} )
	CPanel:AddControl( "Checkbox", {
		Label = "Melon Gravity toggle",
		Description = "Uncheck box to make melons have no gravity",
		Command = "barrax_melongravity"
	} )
	CPanel:AddControl( "Checkbox", {
		Label = "Weld",
		Description = "Uncheck box and barracks will not be attached to what you shoot at",
		Command = "barrax_welded"
	} )
end
